package com.stadium.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.stadium.dto.CreatFaceDto;
import com.stadium.service.FaceRegSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: smartgame-rugby
 * @description:
 * @author: firstSeven
 * @create: 2020/07/15
 */
@Service
@Transactional
public class FaceRegSVI implements FaceRegSV {

    private final String method = "1";

    @Autowired
    RestTemplate restTemplate;

    @Value("${inner-api.file-url-convert-batch}")
    private String fileUrlConvertBatch;

    @Override
    public CreatFaceDto fileUrlConvertDface(String profilePhoto) {
        List<String> profilePhotos = new ArrayList<>();
        profilePhotos.add(profilePhoto);
        List<CreatFaceDto> creatFaceDtos = fileUrlConvertBatchDface(profilePhotos);
        return creatFaceDtos.get(0);
    }

    @Override
    public List<CreatFaceDto> fileUrlConvertBatchDface(List<String> profilePhotos) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("fileUrls", profilePhotos);
        jsonObject.put("method", method);
        jsonObject.put("urlType","0");
        JSONObject json = restTemplate.postForEntity(fileUrlConvertBatch, jsonObject, JSONObject.class).getBody();
        List<CreatFaceDto> creatFaceDtos = json.getJSONArray("data").toJavaList(CreatFaceDto.class);
        return creatFaceDtos;
    }

}
